package com.yidu.BusinessManagement.service.impl;

import com.google.gson.Gson;
import com.yidu.BusinessManagement.entity.DealData;
import com.yidu.BusinessManagement.mapper.DealDataMapper;
import com.yidu.BusinessManagement.service.DealDataService;
import com.yidu.BusinessParament.entity.Bond;
import com.yidu.BusinessParament.entity.ExchangeRate;
import com.yidu.CashManagement.util.CreateId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 苏彬
 * @date 2022/12/9 14:07
 **/
@Service
public class DealDataImpl implements DealDataService {
    @Autowired
    private DealDataMapper mapper;

    /**
     * 查询交易数据
     *
     * @param page         当前页
     * @param row          显示页
     * @param dealTime     交易时间
     * @param securityName 证卷名称
     * @return layuiMap
     */
    @Override
    public Map selectDealDate(int page, int row, String dealTime, String securityName) {
        //获取sql语句
        String term = append(dealTime, securityName).toString();
        //创建个空map
        Map selectMap = new HashMap();
        //传入当前页
        selectMap.put("page", (page - 1) * row);
        //传入显示页
        selectMap.put("row", row);
        //传入条件
        selectMap.put("term", term);

        //创建layui的空map
        Map layUiMap = new HashMap();
        //传入data数据集合
        layUiMap.put("data", mapper.selectDealDate(selectMap));
        //传入msg消息
        layUiMap.put("msg", "");
        //传入code状态码
        layUiMap.put("code", 0);
        //传入总行数count
        layUiMap.put("count", mapper.selectDealDateCount(selectMap));

        //返回layuiMap
        return layUiMap;
    }

    /**
     * 删除交易数据
     *
     * @param myArr
     */
    @Override
    public void deleteDealDate(String myArr) {
        //把传来的Json用Gson转字符数组
        String[] array = new Gson().fromJson(myArr, String[].class);
        //循环数组根据id删除交易数据
        for (String Id : array) {
            mapper.deleteDealDate(Id);
        }
    }

    /**
     * 添加交易数据
     *
     * @param data 交易数据对象
     * @return int 执行行数
     */
    @Override
    public int addDealDate(DealData data) {
        //调用CreateId创建交易Id
        String Id = CreateId.createId("TD");
        //给date对象赋Id
        data.setDealId(Id);
        //设置交易状态默认为0
        data.setDealState(0);
        //赋值基金Id
        data.setFundId("159934");
        //返回执行行数
        return mapper.addDealDate(data);
    }

    /**
     * 修改交易数据
     *
     * @param data 交易对象
     */
    @Override
    public void updateDealDate(DealData data) {
        //执行交易对象修改
        mapper.updateDealDate(data);
    }

    /**
     * 查询证券
     *
     * @return Map
     */
    @Override
    public Map selectSecurity() {
        //创建空map
        Map map = new HashMap();
        //传入map
        map.put("msg", "");
        //传入行数
        map.put("count", mapper.selectSecurityCount());
        //传入状态码
        map.put("code", 0);
        //传入数据集合
        map.put("data", mapper.selectSecurity());
        //返回map
        return map;
    }

    /**
     * 查询券商
     *
     * @return
     */
    @Override
    public Map selectBroker() {
        //创建空map
        Map map = new HashMap();
        //传入msg
        map.put("msg", "");
        //传入count总行数
        map.put("count", mapper.selectBrokerCount());
        //传入状态
        map.put("code", 0);
        //传入数据格式
        map.put("data", mapper.selectBroker());
        //传入map
        return map;
    }

    /**
     * 查询交易数据
     *
     * @return Map
     */
    @Override
    public Map selectDealData() {
        //创建map
        Map map = new HashMap();
        //传入msg
        map.put("msg", "");
        //传入count
        map.put("count", mapper.selectDealSeatCount());
        //传入code
        map.put("code", 0);
        //传入data
        map.put("data", mapper.selectDealSeat());
        //返回map
        return map;
    }

    /**
     * 查询债卷
     * @param securityId 证卷id
     * @return 债卷集合
     */
    @Override
    public Bond selectBondd(String securityId) {
        //返回债卷集合
        return mapper.selectBondd(securityId);
    }

    /**
     * 查询交易席位佣金费用
     * @param seatId 席位id
     * @return  费用
     */
    @Override
    public String selectCommission(String seatId) {
        //返回费用
        return mapper.selectCommission(seatId);
    }

    /**
     * 查询交易品种费率
     * @param securityId 证券Id
     * @return  交易品种费率
     */
    @Override
    public ExchangeRate ExchangeRateMoney(String securityId) {
        //返回交易品种费率
        return mapper.ExchangeRateMoney(securityId);
    }


    /**
     * 拼接sql 语句
     *
     * @param dealTime     交易时间
     * @param securityName 卷商名称
     * @return StringBuffer
     */
    public StringBuffer append(String dealTime, String securityName) {
        //创建空StringBuffer对象
        StringBuffer term = new StringBuffer();
        //判断dealTime是否为空拼接sql语句
        if (dealTime != null && !dealTime.equals("")) {
            term.append(" and dealTime='").append(dealTime).append("'");
        }
        //判断securityName是否为空拼接sql模糊查语句
        if (securityName != null && !securityName.equals("")) {
            term.append(" and s.`securityName` like '%").append(securityName).append("%'");
        }
        //返回term
        return term;
    }
}
